/*
 *  WilBase64XferBytes.h
 *  Wilson
 *
 *  Created by Sebastian Orfino on 8/23/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef __WILBASE64XFERBYTES__
#define __WILBASE64XFERBYTES__


#include "IXferBytes.h"
#include <sstream>
#include <string>
#include <vector>

/** Esta clase implementa un buffer que mantiene lo que se escribe en el codificado como base64 */
class WilBase64XferBytes : public IXferBytes {

public:
	
	WilBase64XferBytes();
	virtual ~WilBase64XferBytes();
	
	//--- The IXferBytes interface we must implement  ---
	
	/** See IXferBytes::Read
	 */
	virtual uint32 Read(void* buffer, uint32 num);
	
	/** See IXferBytes::Write
	 */
	virtual uint32 Write(void* buffer, uint32 num);
	
	/** See IXferBytes::Seek
	 */
	virtual uint32 Seek(int32 numberOfBytes, SeekFromWhere fromHere);
	
	/** See IXferBytes::Flush
	 */
	virtual void Flush(void);
	
	/** See IXferBytes::GetStreamState
	 */
	virtual StreamState GetStreamState(void);
	
	/** See IXferBytes::SetEndOfStream
	 */
	virtual void SetEndOfStream(void);
	
	/** Te devuelve lo que se escrbio en el buffer hasta ahora representado como un string en base64 */
	const std::string GetBuffer() const { return buffer_stream_.str(); }
	
private:
	
	static char Encode(uchar uc);
	static uchar Decode(char c);
	static bool IsBase64(char c);
	
	static std::string Encode(const std::vector<uchar>& v);
	static std::vector<uchar> Decode(const std::string& s);
	
	//std::string buffer_;
	std::stringstream buffer_stream_;
	std::size_t buffer_pos_;
	std::string hasta_ahora;
	
	std::size_t largo_buffer() ;
	
};

#endif //__WILBASE64XFERBYTES__